//
//  AssetScrollView.m
//  SecretAlbums
//
//  Created by BabyFinancial on 15/6/26.
//  Copyright (c) 2015年 BabyFinancial. All rights reserved.
//

#import "PhotoScrollView.h"
#import <MediaPlayer/MediaPlayer.h>
#import "Media.h"
#import "SecretAlbumsLogic.h"
#import "UIImage+CTAssetsPickerController.h"
#import "Masonry.h"

@interface PhotoScrollView () <UIScrollViewDelegate>

@property (nonatomic, assign) CGFloat        defaultScale;

@end

@implementation PhotoScrollView

- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.showsVerticalScrollIndicator   = NO;
        self.showsHorizontalScrollIndicator = NO;
        self.bounces                        = NO;
        self.bouncesZoom                    = YES;
        self.decelerationRate               = UIScrollViewDecelerationRateFast;
        self.delegate                       = self;
    }
    
    return self;
}

- (void)setIndex:(NSUInteger)index
{
    _index = index;
    [self displayImageAtIndex:index];
}

- (void)setFrame:(CGRect)frame
{
    if (!CGRectEqualToRect(self.frame, frame)) {
        [super setFrame:frame];
        if (!CGSizeEqualToSize(self.imageView.bounds.size, CGSizeZero)) {
            [self setMinMaxAndZoomScale];
        }
    }
}

/** 双击时的放大缩小处理 */
- (void)toggleZooming:(UITapGestureRecognizer *)recognizer
{
    if (self.zoomScale == self.defaultScale) {
        CGRect zoomRect = [self zoomRectWithScale:self.maximumZoomScale withCenter:[recognizer locationInView:recognizer.view]];
        [self zoomToRect:zoomRect animated:YES];
    }else {
        [self setZoomScale:self.defaultScale animated:YES];
    }
}

#pragma mark - 私有方法

- (void)displayImageAtIndex:(NSUInteger)index
{
    if ([self.dataSource respondsToSelector:@selector(mediaAtIndex:)]) {
        Media *media = [self.dataSource mediaAtIndex:index];
        
        self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, media.width.floatValue, media.height.floatValue)];
        self.imageView.image = [UIImage imageWithContentsOfFile:[SecretAlbumsLogic pathWithMediaType:MediaTypeAspectRatioThumbnail name:media.fileName]];
        [self addSubview:self.imageView];
        [self setMinMaxAndZoomScale];
    }
}

- (void)setMinMaxAndZoomScale
{
    self.minimumZoomScale = 0.5f;
    self.maximumZoomScale = 1.5f;
    CGFloat scale = (int)((self.bounds.size.width/self.imageView.bounds.size.width) *1000) / 1000.0f;
    
    if (scale < self.minimumZoomScale) {
        self.minimumZoomScale = scale;
    }
    
    if (scale > self.maximumZoomScale) {
        self.maximumZoomScale = scale;
    }
    
    self.zoomScale = scale;
    self.contentOffset = CGPointZero;
    self.defaultScale = scale;
}

- (CGRect)zoomRectWithScale:(float)scale withCenter:(CGPoint)center
{
    // 获取imageView的原始大小
    CGSize originalSize = CGSizeMake(self.imageView.frame.size.width / self.defaultScale, self.imageView.frame.size.height / self.defaultScale);
    
    // 获取合适的区域
    CGFloat width = self.bounds.size.width < originalSize.width ? self.bounds.size.width : originalSize.width;
    CGFloat height = self.bounds.size.height < originalSize.height ? self.bounds.size.height : originalSize.height;
    
    // 除以放大倍数
    width = width / scale;
    height = height / scale;
    
    CGRect zoomRect;
    zoomRect.size.width = width;
    zoomRect.size.height = height;
    
    center = [self.imageView convertPoint:center fromView:self];
    
    zoomRect.origin.x    = center.x - (width / 2.0);
    zoomRect.origin.y    = center.y - (height / 2.0);
    
    return zoomRect;
}

#pragma mark - UIScrollViewDelegate

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return self.imageView;
}

- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
    CGFloat xcenter = scrollView.center.x;
    CGFloat ycenter = scrollView.center.y;
    
    xcenter = scrollView.contentSize.width > scrollView.frame.size.width ? scrollView.contentSize.width/2 : xcenter;
    ycenter = scrollView.contentSize.height > scrollView.frame.size.height ? scrollView.contentSize.height/2 : ycenter;
    
    [self.imageView setCenter:CGPointMake(xcenter, ycenter)];
}

@end
